package com.zlll.winner.service.mall;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zlll.winner.business.model.mall.MallGoodsDetails;
import com.zlll.winner.mapper.mall.MallGoodsDetailsMapper;
import com.zlll.winner.business.service.mall.IMallGoodsDetailsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 积分商品
 服务实现类
 * </p>
 *
 * @author qsx
 * @since 2019-10-08
 */
@Service
public class MallGoodsDetailsServiceImpl extends ServiceImpl<MallGoodsDetailsMapper, MallGoodsDetails> implements IMallGoodsDetailsService {

    @Autowired
    private MallGoodsDetailsMapper goodsDetailsMapper;

    @Override
    public MallGoodsDetails saveMallGoodsDetails(MallGoodsDetails mallGoodsDetails) {
        mallGoodsDetails.setCreateTime(System.currentTimeMillis());
        mallGoodsDetails.setUpdateTime(System.currentTimeMillis());
        mallGoodsDetails.setIsDel(0);
        save(mallGoodsDetails);
        return mallGoodsDetails;
    }

    @Override
    public IPage<MallGoodsDetails> getMallGoodsDetails(String goodsName, Integer pageSize, Integer pageNo) {
        IPage<MallGoodsDetails> page = new Page<>(pageNo,pageSize);
        LambdaQueryWrapper<MallGoodsDetails> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(MallGoodsDetails::getIsDel,0);
        if(goodsName != null){
            queryWrapper.like(MallGoodsDetails::getGoodsName,goodsName);
        }
        return goodsDetailsMapper.selectPage(page,queryWrapper);
    }

    @Override
    public MallGoodsDetails editMallGoodsDetails(MallGoodsDetails mallGoodsDetails) {
        mallGoodsDetails.setUpdateTime(System.currentTimeMillis());
        updateById(mallGoodsDetails);
        return mallGoodsDetails;
    }

    @Override
    public Boolean deleteById(Long id,Long userId) {
        MallGoodsDetails mallGoodsDetails = goodsDetailsMapper.selectById(id);
        mallGoodsDetails.setUpdateTime(System.currentTimeMillis());
        mallGoodsDetails.setUpdateUser(userId+"");
        mallGoodsDetails.setIsDel(1);
        return updateById(mallGoodsDetails);
    }

    @Override
    public IPage<MallGoodsDetails> getReleaseMallGoodsDetails(String goodsName, Integer pageSize, Integer pageNo) {
        IPage<MallGoodsDetails> page = new Page<>(pageNo,pageSize);
        LambdaQueryWrapper<MallGoodsDetails> queryWrapper = Wrappers.lambdaQuery();

        queryWrapper.ge(MallGoodsDetails::getStatus,1);
        if(goodsName != null){
            queryWrapper.like(MallGoodsDetails::getGoodsName,goodsName);
        }
        queryWrapper.eq(MallGoodsDetails::getIsDel,0);
        return goodsDetailsMapper.selectPage(page,queryWrapper);    }

    @Override
    public Boolean setPutaway(Long goodsId) {
        MallGoodsDetails mallGoodsDetails = goodsDetailsMapper.selectById(goodsId);
        mallGoodsDetails.setStatus(2);
        mallGoodsDetails.setUpdateTime(System.currentTimeMillis());
        return updateById(mallGoodsDetails);
    }
}
